using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System;
	using System.Web.Mvc;
	using System.Web.Security;
	using System.Collections.Generic;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models.BUS;
	using TemplateWeb.Helpers;
	using TemplateWeb.Models;
	using System.IO;
	using System.Web;
    using TemplateWeb.DTO;
	#endregion
	public abstract class BaseController : Controller
	{
		protected HinhThucDauGiaBUS _repHinhThucDauGia = new HinhThucDauGiaBUS();
		protected HopThuBUS _repHopThu = new HopThuBUS();
		protected PhanHoiBUS _repPhanHoi = new PhanHoiBUS();
		protected HuongDanBUS _repHuongDan = new HuongDanBUS();
		protected PhienDauGiaBUS _repPhienDauGia = new PhienDauGiaBUS();
		protected DanhMucChinhBUS _repDanhMucChinh = new DanhMucChinhBUS();
		protected NguoiChoiDauGiaBUS _repNguoiChoiDauGia = new NguoiChoiDauGiaBUS();
		protected DanhMucConBUS _repDanhMucCon = new DanhMucConBUS();
		protected NguoiDungBUS _repNguoiDung = new NguoiDungBUS();
        protected ToChucCaNhanBUS _repToChucCaNhan = new ToChucCaNhanBUS();
        protected SanPhamBUS _repSanPham = new SanPhamBUS();

		protected string RenderPartialViewToString()
		{
			return RenderPartialViewToString(null, null);
		}

		protected string RenderPartialViewToString(string viewName)
		{
			return RenderPartialViewToString(viewName, null);
		}

        protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");
            ViewData.Model = model;
            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                return sw.GetStringBuilder().ToString();
            }
        }

		public GroupViewModel CreateViewModel(DataTransferViewModel dataTransfer)
		{
			GroupViewModel viewModel = new GroupViewModel();
			if (dataTransfer.EnumViewModelType.ToString().Contains("ADMIN"))
			{
                viewModel.DanhMucChinhModel = RetrieveDanhMucChinhViewModel(dataTransfer);
            }
            else if (dataTransfer.EnumViewModelType.ToString().Contains("HOME"))
            {
                viewModel.DanhMucChinhModel = RetrieveDanhMucChinhViewModel(dataTransfer);
            }
			switch (dataTransfer.EnumViewModelType)
			{
				case EnumViewModel.ADMIN_DETAILOF_HOPTHU:
					break;

				case EnumViewModel.ADMIN_DETAILOF_PHANHOI:
                    viewModel.PhanHoiModel = RetrieveDetailOfPhanHoiViewModel(dataTransfer);
					break;

				case EnumViewModel.ADMIN_DETAILOF_HUONGDAN:
                    viewModel.HuongDanModel = RetrieveDetailOfHuongDanViewModel(dataTransfer);
					break;

				case EnumViewModel.ADMIN_DETAILOF_PHIENDAUGIA:
                    viewModel.PhienDauGiaModel = RetrieveDetailOfPhienDauGiaViewModel(dataTransfer);
					break;

				case EnumViewModel.ADMIN_DETAILOF_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveDetailOfNguoiDungViewModel(dataTransfer);
					break;

				case EnumViewModel.ADMIN_HINHTHUCDAUGIA:
					break;

				case EnumViewModel.ADMIN_HOPTHU:
                    viewModel.HopThuModel = RetrieveHopThuViewModel(dataTransfer);
					break;
				
                case EnumViewModel.ADMIN_PHANHOI:
                    viewModel.PhanHoiModel = RetrievePhanHoiViewModel(dataTransfer);
                    viewModel.HopThuModel = RetrieveHopThuViewModel(dataTransfer);
					break;
				
                case EnumViewModel.ADMIN_HUONGDAN:
                    viewModel.HuongDanModel = RetrieveHuongDanViewModel(dataTransfer);
					break;
				
                case EnumViewModel.ADMIN_PHIENDAUGIA:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaViewModel(dataTransfer);
					break;

                case EnumViewModel.ADMIN_CREATEPHIENDAUGIA:
                    viewModel.SanPhamModel = RetrieveDetailOfSanPhamViewModel(dataTransfer);
                    viewModel.HinhThucDauGiaModel = RetrieveHinhThucDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.ADMIN_CREATEPHIENDAUGIA_SUCCESSFUL:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaViewModel(dataTransfer);
                    break;
				
                case EnumViewModel.ADMIN_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveNguoiDungViewModel(dataTransfer);
					break;
				
                case EnumViewModel.ADMIN_DANHMUCCHINH:
					break;
				
                case EnumViewModel.ADMIN_NGUOICHOIDAUGIA:
					break;
				
                case EnumViewModel.ADMIN_DANHMUCCON:
                    viewModel.DanhMucConModel = RetrieveDanhMucConViewModel(dataTransfer);
					break;

				case EnumViewModel.ADMIN_TOCHUCCANHAN:
                    viewModel.ToChucCaNhanModel = RetrieveToChucCaNhanViewModel(dataTransfer);
                    break;

                case EnumViewModel.ADMIN_SANPHAM:
                    viewModel.SanPhamModel = RetrieveSanPhamViewModel(dataTransfer);
                    viewModel.ToChucCaNhanModel = RetrieveToChucCaNhanViewModel(dataTransfer);
                    break;

                case EnumViewModel.ADMIN_DETAILOF_SANPHAM:
                    viewModel.SanPhamModel = RetrieveDetailOfSanPhamViewModel(dataTransfer);
                    viewModel.ToChucCaNhanModel = RetrieveToChucCaNhanViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_INDEX:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_REGISTER:
					break;
				
                case EnumViewModel.HOME_DETAILOF_NGUOIDUNG:
                    viewModel.NguoiDungModel = RetrieveDetailOfNguoiDungViewModel(dataTransfer);
					break;
                
                case EnumViewModel.HOME_XEMCACPHIEN:
                    viewModel.SelectedDanhMucChinhModel = RetrieveSelectedDanhMucChinhViewModel(dataTransfer);
                    viewModel.DanhMucConModel = RetrieveDanhMucConViewModel(dataTransfer);
                    break;
                
                case EnumViewModel.HOME_TAOPHIENMOI:
                    viewModel.HinhThucDauGiaModel = RetrieveHinhThucDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_TAOPHIENMOI_SUBMITTED:
                    viewModel.HinhThucDauGiaModel = RetrieveHinhThucDauGiaViewModel(dataTransfer);
                    break;
                
                case EnumViewModel.HOME_XEMCACPHIENTHEOHANGMUC:
                    viewModel.SelectedDanhMucChinhModel = RetrieveSelectedDanhMucChinhViewModel(dataTransfer);
                    viewModel.SelectedDanhMucConModel = RetrieveSelectedDanhMucConViewModel(dataTransfer);
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_DATCHODAUGIA:
                    viewModel.PhienDauGiaModel = RetrieveDetailOfPhienDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_XEMTATCACACPHIEN:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_XEMPHIENDATAO:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_XEMCHITIETPHIEN:
                    viewModel.PhienDauGiaModel = RetrieveDetailOfPhienDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_DAUGIACUATOI:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaCuaToiViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_CACPHIENTHANHCONG:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaThanhCongViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_CACPHIENTHANGCUOC:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaThangCuocViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_CACPHIENCUNGNGUOIDANG:
                    viewModel.PhienDauGiaModel = RetrievePhienDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_TIMKIEM:
                    viewModel.NguoiDungModel = RetrieveNguoiBanViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_TIMKIEM_SUBMITTED:
                    viewModel.PhienDauGiaModel = TimKiemPhienDauGiaViewModel(dataTransfer);
                    break;

                case EnumViewModel.HOME_HUONGDAN:
                    viewModel.HuongDanModel = RetrieveHuongDanViewModel(dataTransfer);
                    break;

			}
			return viewModel;
		}

		public bool UploadFile(HttpPostedFileBase file, string path, ref string refName)
		{
			try
			{
				if (!System.IO.File.Exists(Server.MapPath(path + file.FileName)))
				{
					FileInfo f = new FileInfo(file.FileName);
					long ngay = DateTime.Now.ToBinary();
					refName = ngay + f.Extension;
					string fullPath = path + refName;
					if (file == null)
					    return false;
					byte[] buffer = new byte[file.ContentLength];
					file.InputStream.Read(buffer, 0, file.ContentLength);
					FileStream newFile = new FileStream(Server.MapPath(fullPath), FileMode.Create, FileAccess.Write);
					newFile.Write(buffer, 0, file.ContentLength);
					newFile.Close();
				}
				else
				{
					refName = file.FileName;
				}
				return true;
			}
			catch
			{
				return false;
			}
		}

		public void DeleteFile(string filePath)
		{
			FileInfo file = new FileInfo(filePath);
			if (file.Exists)
			{
				file.Delete();
			}
		}

		public bool SendingMail(string subject, string emailContent, string to)
		{
			bool result = false;
			try
			{
				System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient();
				System.Net.NetworkCredential auth = new System.Net.NetworkCredential(WebConfiguration.EmailLienHe, WebConfiguration.PasswordEmailLienHe);
				mailclient.Host = WebConfiguration.MailServer;
				mailclient.EnableSsl = true;
				mailclient.UseDefaultCredentials = true;
				mailclient.Credentials = auth;
				System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(WebConfiguration.EmailLienHe, to);
				mm.Subject = subject;
				mm.IsBodyHtml = true;
				mm.Body = emailContent;
				mailclient.Send(mm);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

        public SanPhamViewModel RetrieveDetailOfSanPhamViewModel(DataTransferViewModel dataTransfer)
        {
            return new SanPhamViewModel
            {
                GetModel = new GetSanPhamViewModel
                {
                    LstObjModel = _repSanPham.RetrieveByID(dataTransfer.IntID)
                },
                EditModel = new EditSanPhamViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public SanPhamViewModel RetrieveSanPhamViewModel(DataTransferViewModel dataTransfer)
        {
            List<SanPhamDTO> lst = null;
            int totalitem = -1;
            if (dataTransfer.IDDanhMucCon != -1)
            {
                lst = _repSanPham.SelectByIDDanhMucCon(dataTransfer.IDDanhMucCon);
            }
            if (dataTransfer.IDToChucCaNhan != -1)
            {
                lst = _repSanPham.SelectByIDToChucCaNhan(dataTransfer.IDToChucCaNhan);
            }
            else
            {
                lst = _repSanPham.SelectAll();
            }
            totalitem = lst.Count;
            return new SanPhamViewModel
            {
                GetModel = new GetSanPhamViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddSanPhamViewModel
                {
                },
                EditModel = new EditSanPhamViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new SanPhamReferKeys()
                {
                    IDDanhMucCon = dataTransfer.IDDanhMucCon,
                    IDToChucCaNhan = dataTransfer.IDToChucCaNhan,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public ToChucCaNhanViewModel RetrieveToChucCaNhanViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repToChucCaNhan.SelectAll();
            return new ToChucCaNhanViewModel
            {
                GetModel = new GetToChucCaNhanViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddToChucCaNhanViewModel
                {
                },
                EditModel = new EditToChucCaNhanViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public PhanHoiViewModel RetrieveDetailOfPhanHoiViewModel(DataTransferViewModel dataTransfer)
        {
            List<PhanHoiDTO> lst = null;
            int totalitem = -1;

            lst = _repPhanHoi.RetrieveByID(dataTransfer.IntID);
            totalitem = lst.Count;

            return new PhanHoiViewModel
            {
                GetModel = new GetPhanHoiViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhanHoiViewModel
                {
                },
                EditModel = new EditPhanHoiViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhanHoiReferKeys()
                {
                    IDHopThu = dataTransfer.IDHopThu,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public HinhThucDauGiaViewModel RetrieveHinhThucDauGiaViewModel(DataTransferViewModel dataTransfer)
		{
            var lst = _repHinhThucDauGia.SelectAll();
			return new HinhThucDauGiaViewModel
			{
				GetModel = new GetHinhThucDauGiaViewModel
				{
                    LstObjModel = lst,
                    TotalItem = lst.Count,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddHinhThucDauGiaViewModel
				{
				},
				EditModel = new EditHinhThucDauGiaViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public HopThuViewModel RetrieveHopThuViewModel(DataTransferViewModel dataTransfer)
		{
            var lst = _repHopThu.SelectAll();
			return new HopThuViewModel
			{
				GetModel = new GetHopThuViewModel
				{
					LstObjModel = lst,
					TotalItem = lst.Count,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddHopThuViewModel
				{
				},
				EditModel = new EditHopThuViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public PhanHoiViewModel RetrievePhanHoiViewModel(DataTransferViewModel dataTransfer)
		{
			List<PhanHoiDTO> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDHopThu != -1)
			{
				lst = _repPhanHoi.SelectByIDHopThu(dataTransfer.IDHopThu);
                totalitem = lst.Count;
			}
			else
			{
				lst = _repPhanHoi.SelectAll();
				totalitem = lst.Count;
			}
			return new PhanHoiViewModel
			{
				GetModel = new GetPhanHoiViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddPhanHoiViewModel
				{
				},
				EditModel = new EditPhanHoiViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new PhanHoiReferKeys()
				{
					IDHopThu = dataTransfer.IDHopThu,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public HuongDanViewModel RetrieveHuongDanViewModel(DataTransferViewModel dataTransfer)
		{
            var lst = _repHuongDan.SelectAll();
			return new HuongDanViewModel
			{
				GetModel = new GetHuongDanViewModel
				{
					LstObjModel = lst,
					TotalItem = lst.Count,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddHuongDanViewModel
				{
				},
				EditModel = new EditHuongDanViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public PhienDauGiaViewModel RetrievePhienDauGiaCuaToiViewModel(DataTransferViewModel dataTransfer)
        {
            List<PhienDauGiaDTO> lst = _repPhienDauGia.SelectByIDNguoiDauGia(dataTransfer.IDNguoiDung);
            int totalitem = lst.Count;

            return new PhienDauGiaViewModel
            {
                GetModel = new GetPhienDauGiaViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhienDauGiaViewModel
                {
                },
                EditModel = new EditPhienDauGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhienDauGiaReferKeys()
                {
                    IDHinhThucDauGia = dataTransfer.IDHinhThucDauGia,
                    IDDanhMucCon = dataTransfer.IDDanhMucCon,
                    IDNguoiDang = dataTransfer.IDNguoiDang,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public PhienDauGiaViewModel RetrievePhienDauGiaThangCuocViewModel(DataTransferViewModel dataTransfer)
        {
            List<PhienDauGiaDTO> lst = _repPhienDauGia.SelectPhienThangCuoc(dataTransfer.IDNguoiDung);
            int totalitem = lst.Count;

            return new PhienDauGiaViewModel
            {
                GetModel = new GetPhienDauGiaViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhienDauGiaViewModel
                {
                },
                EditModel = new EditPhienDauGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhienDauGiaReferKeys()
                {
                    IDHinhThucDauGia = dataTransfer.IDHinhThucDauGia,
                    IDDanhMucCon = dataTransfer.IDDanhMucCon,
                    IDNguoiDang = dataTransfer.IDNguoiDang,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public PhienDauGiaViewModel RetrievePhienDauGiaThanhCongViewModel(DataTransferViewModel dataTransfer)
        {
            List<PhienDauGiaDTO> lst = _repPhienDauGia.SelectPhienThanhCong();
            int totalitem = lst.Count;

            return new PhienDauGiaViewModel
            {
                GetModel = new GetPhienDauGiaViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhienDauGiaViewModel
                {
                },
                EditModel = new EditPhienDauGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhienDauGiaReferKeys()
                {
                    IDHinhThucDauGia = dataTransfer.IDHinhThucDauGia,
                    IDDanhMucCon = dataTransfer.IDDanhMucCon,
                    IDNguoiDang = dataTransfer.IDNguoiDang,
                },
                InfoText = dataTransfer.InfoText
            };
        }

        public PhienDauGiaViewModel TimKiemPhienDauGiaViewModel(DataTransferViewModel dataTransfer)
        {
            List<PhienDauGiaDTO> lst = _repPhienDauGia.TimKiem(dataTransfer.TimKiem_TieuDe, dataTransfer.TimKiem_TimTrongMoTa, dataTransfer.TimKiem_ThucMuc, dataTransfer.TimKiem_NguoiBan, dataTransfer.TimKiem_BatDauSauNgay, dataTransfer.TimKiem_KetThucTruocNgay);
            int totalitem = lst.Count;

            return new PhienDauGiaViewModel
            {
                GetModel = new GetPhienDauGiaViewModel
                {
                    LstObjModel = lst,
                    TotalItem = totalitem,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddPhienDauGiaViewModel
                {
                },
                EditModel = new EditPhienDauGiaViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new PhienDauGiaReferKeys()
                {
                    IDHinhThucDauGia = dataTransfer.IDHinhThucDauGia,
                    IDDanhMucCon = dataTransfer.IDDanhMucCon,
                    IDNguoiDang = dataTransfer.IDNguoiDang,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public PhienDauGiaViewModel RetrievePhienDauGiaViewModel(DataTransferViewModel dataTransfer)
		{
			List<PhienDauGiaDTO> lst = null;
			int totalitem = -1;

			if (dataTransfer.IDHinhThucDauGia != -1)
			{
				lst = _repPhienDauGia.SelectByIDHinhThucDauGia(dataTransfer.IDHinhThucDauGia);
				totalitem = lst.Count;
			}
            else if (dataTransfer.IDDanhMucCon != -1)
            {
                lst = _repPhienDauGia.SelectByIDDanhMucCon(dataTransfer.IDDanhMucCon);
                totalitem = lst.Count;
            }
			else if (dataTransfer.IDDanhMucChinh != -1)
			{
                lst = _repPhienDauGia.SelectByIDDanhMucChinh(dataTransfer.IDDanhMucChinh);
                totalitem = lst.Count;
			}
			else if (dataTransfer.IDNguoiDang != Guid.Parse("11112222-1111-1111-1111-111122223333"))
			{
				lst = _repPhienDauGia.SelectByIDNguoiDang(dataTransfer.IDNguoiDang);
                totalitem = lst.Count;
			}
			else
			{
				lst = _repPhienDauGia.SelectAll();
				totalitem = lst.Count;
			}

			return new PhienDauGiaViewModel
			{
				GetModel = new GetPhienDauGiaViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddPhienDauGiaViewModel
				{
				},
				EditModel = new EditPhienDauGiaViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new PhienDauGiaReferKeys()
				{
					IDHinhThucDauGia = dataTransfer.IDHinhThucDauGia,
					IDDanhMucCon = dataTransfer.IDDanhMucCon,
					IDNguoiDang = dataTransfer.IDNguoiDang,
				},
				InfoText = dataTransfer.InfoText
			};
		}

		public DanhMucChinhViewModel RetrieveDanhMucChinhViewModel(DataTransferViewModel dataTransfer)
		{
            var lst = _repDanhMucChinh.SelectAll();
			return new DanhMucChinhViewModel
			{
				GetModel = new GetDanhMucChinhViewModel
				{
                    LstObjModel = lst,
                    TotalItem = lst.Count,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddDanhMucChinhViewModel
				{
				},
				EditModel = new EditDanhMucChinhViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public DanhMucChinhViewModel RetrieveSelectedDanhMucChinhViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repDanhMucChinh.RetrieveByID(dataTransfer.IDDanhMucChinh);
            return new DanhMucChinhViewModel
            {
                GetModel = new GetDanhMucChinhViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDanhMucChinhViewModel
                {
                },
                EditModel = new EditDanhMucChinhViewModel
                {
                    ID = dataTransfer.IntID,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public NguoiChoiDauGiaViewModel RetrieveNguoiChoiDauGiaViewModel(DataTransferViewModel dataTransfer)
		{
			List<NguoiChoiDauGiaDTO> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDNguoiDung != Guid.Parse("11112222-1111-1111-1111-111122223333"))
			{
				lst = _repNguoiChoiDauGia.SelectByIDNguoiDung(dataTransfer.IDNguoiDung);
                totalitem = lst.Count;
			}
			else if (dataTransfer.IDPhienDauGia != -1)
			{
				lst = _repNguoiChoiDauGia.SelectByIDPhienDauGia(dataTransfer.IDPhienDauGia);
                totalitem = lst.Count;
			}
			else
			{
				lst = _repNguoiChoiDauGia.SelectAll();
				totalitem = lst.Count;
			}
			return new NguoiChoiDauGiaViewModel
			{
				GetModel = new GetNguoiChoiDauGiaViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddNguoiChoiDauGiaViewModel
				{
				},
				EditModel = new EditNguoiChoiDauGiaViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new NguoiChoiDauGiaReferKeys()
				{
					IDNguoiDung = dataTransfer.IDNguoiDung,
					IDPhienDauGia = dataTransfer.IDPhienDauGia,
				},
				InfoText = dataTransfer.InfoText
			};
		}

        public DanhMucConViewModel RetrieveSelectedDanhMucConViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repDanhMucCon.RetrieveByID(dataTransfer.IDDanhMucCon);
            
            return new DanhMucConViewModel
            {
                GetModel = new GetDanhMucConViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddDanhMucConViewModel
                {
                },
                EditModel = new EditDanhMucConViewModel
                {
                    ID = dataTransfer.IntID,
                },
                ReferKeys = new DanhMucConReferKeys()
                {
                    IDDanhMucChinh = dataTransfer.IDDanhMucChinh,
                },
                InfoText = dataTransfer.InfoText
            };
        }

		public DanhMucConViewModel RetrieveDanhMucConViewModel(DataTransferViewModel dataTransfer)
		{
			List<DanhMucConDTO> lst = null;
			int totalitem = -1;
			if (dataTransfer.IDDanhMucChinh != -1)
			{
				lst = _repDanhMucCon.SelectByIDDanhMucChinh(dataTransfer.IDDanhMucChinh);
                totalitem = lst.Count;
			}
			else
			{
				lst = _repDanhMucCon.SelectAll();
				totalitem = lst.Count;
			}
			return new DanhMucConViewModel
			{
				GetModel = new GetDanhMucConViewModel
				{
					LstObjModel = lst,
					TotalItem = totalitem,
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddDanhMucConViewModel
				{
				},
				EditModel = new EditDanhMucConViewModel
				{
					ID = dataTransfer.IntID,
				},
				ReferKeys = new DanhMucConReferKeys()
				{
					IDDanhMucChinh = dataTransfer.IDDanhMucChinh,
				},
				InfoText = dataTransfer.InfoText
			};
		}


		public NguoiDungViewModel RetrieveNguoiDungViewModel(DataTransferViewModel dataTransfer)
		{
			return new NguoiDungViewModel
			{
				GetModel = new GetNguoiDungViewModel
				{
					LstObjModel = _repNguoiDung.SelectPaging(dataTransfer.CurrentPage, WebConfiguration.NumNguoiDungPerPage, dataTransfer.Role),
					TotalItem = _repNguoiDung.GetTotalItem(dataTransfer.Role),
					CurrentPage = dataTransfer.CurrentPage
				},
				AddModel = new AddNguoiDungViewModel
				{
				},
				EditModel = new EditNguoiDungViewModel
				{
					ID = dataTransfer.GuidID,
				}
			};
		}

        public NguoiDungViewModel RetrieveNguoiBanViewModel(DataTransferViewModel dataTransfer)
        {
            var lst = _repNguoiDung.SelectNguoiBan();
            return new NguoiDungViewModel
            {
                GetModel = new GetNguoiDungViewModel
                {
                    LstObjModel = lst,
                    TotalItem = lst.Count,
                    CurrentPage = dataTransfer.CurrentPage
                },
                AddModel = new AddNguoiDungViewModel
                {
                },
                EditModel = new EditNguoiDungViewModel
                {
                    ID = dataTransfer.GuidID,
                }
            };
        }

		public HuongDanViewModel RetrieveDetailOfHuongDanViewModel(DataTransferViewModel dataTransfer)
		{
			return new HuongDanViewModel
			{
				GetModel = new GetHuongDanViewModel
				{
					LstObjModel = _repHuongDan.RetrieveByID(dataTransfer.IntID)
				},
				EditModel = new EditHuongDanViewModel
				{
					ID = dataTransfer.IntID,
				},
				InfoText = dataTransfer.InfoText
			};
		}
		public PhienDauGiaViewModel RetrieveDetailOfPhienDauGiaViewModel(DataTransferViewModel dataTransfer)
		{
			return new PhienDauGiaViewModel
			{
				GetModel = new GetPhienDauGiaViewModel
				{
					LstObjModel = _repPhienDauGia.RetrieveByID(dataTransfer.IDPhienDauGia)
				},
				EditModel = new EditPhienDauGiaViewModel
				{
                    ID = dataTransfer.IDPhienDauGia,
				},
				InfoText = dataTransfer.InfoText
			};
		}
		public NguoiDungViewModel RetrieveDetailOfNguoiDungViewModel(DataTransferViewModel dataTransfer)
		{
            if (dataTransfer.GuidID == Guid.Parse("11112222-1111-1111-1111-111122223333") ||
                dataTransfer.GuidID == Guid.Parse("00000000-0000-0000-0000-000000000000"))
            {
                var nguoidung = _repNguoiDung.SelectByUserName(dataTransfer.UserName);
                dataTransfer.GuidID = nguoidung.BaseInfo.ID;
            }
			return new NguoiDungViewModel
			{
				GetModel = new GetNguoiDungViewModel
				{
					LstObjModel = _repNguoiDung.RetrieveByID(dataTransfer.GuidID)
				},
				EditModel = new EditNguoiDungViewModel
				{
					ID = dataTransfer.GuidID,
				},
				InfoText = dataTransfer.InfoText
			};
		}

	}
}